package com.wangjw.leetcode;

import java.util.*;

/**
 * Created by wangjw on 18/6/20.
 */
public class LeetCode349 {

    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        List<Integer> list = new ArrayList<>();

        for (int num : nums1) {
            set.add(num);
        }

        for (int num : nums2) {
            if (set.contains(num)) {
                list.add(num);
                set.remove(num);
            }
        }

        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    public static void main(String[] args) {
        LeetCode349 leetCode804 = new LeetCode349();
        int[] nums1= {1, 2, 2, 1};
        int[] nums2 = {2, 2};

        int[] arr = leetCode804.intersection(nums1, nums2);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
